package com.sanjiahantang.youjiarole.module.me.require.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.UserOBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiarole.module.me.order.models.IOrderModel;
import com.sanjiahantang.youjiarole.module.me.order.models.impl.OrderModel;
import com.sanjiahantang.youjiarole.module.me.quotation.views.impl.QuotationActivity;
import com.sanjiahantang.youjiarole.module.me.require.beans.RequireDetailInitResponseBean;
import com.sanjiahantang.youjiarole.module.me.require.beans.RequireListBean;
import com.sanjiahantang.youjiarole.module.me.require.models.IRequireModel;
import com.sanjiahantang.youjiarole.module.me.require.models.impl.RequireModel;
import com.sanjiahantang.youjiarole.module.me.require.views.RequireView;
import com.sanjiahantang.youjiarole.module.me.require.views.impl.RequireDetailActivity;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/10/24.
 */

public class RequirePresenter extends BaseMvpPresenter<RequireView> {

    private IRequireModel requireModel;
    private QuickAdapter<RequireListBean.Item> listAdapter;
    private IOrderModel orderModel;

    private HashMap<Integer,String> stateMap;
    public RequirePresenter(){
        requireModel = new RequireModel();
        orderModel = new OrderModel();
        stateMap = new HashMap<>();
        stateMap.put(0,"待接单");
        stateMap.put(1,"已接单");
        stateMap.put(2,"接单完成");
        stateMap.put(3,"待接关闭");
    }
    public void bindList(Context context,AbsListView listView) {
        if (listAdapter == null){
            listAdapter = new QuickAdapter<RequireListBean.Item>(context, R.layout.item_require_list) {
                @Override
                protected void convert(BaseAdapterHelper helper, final RequireListBean.Item item) {
                    helper.setText(R.id.txtRequireName,item.getApartmentName());
                    helper.setText(R.id.txtCity,item.getAddress());
                    helper.setText(R.id.txtDesignerName,item.getUserName());
                    helper.setText(R.id.txtState,stateMap.get(item.getDesignStateId()));
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(context, RequireDetailActivity.class);
                            intent.putExtra(KEY_PARAM,item);
                            context.startActivity(intent);
                        }
                    });
                }
            };
        }
        listView.setAdapter(listAdapter);
    }

    public void loadRequireList(Context context) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        param.put(KEY_PAGE,0);
        param.put(KEY_PARAM,new HashMap<String,Object>());
        requireModel.loadRequireList(context,param, new CallBack<RequireListBean>() {
            @Override
            public void onSuccess(RequireListBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    listAdapter.clear();
                    if (response.getData() != null){
                        listAdapter.addAll(response.getData().getList());
                    }

                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null){
            mView.hideProgress();
        }
    }

    public void indexUserRequireInfoForRole(final Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(KEY_ID,String.valueOf(id));
        mView.showProgress();
        requireModel.indexUserRequireInfoForRole(context,param, new CallBack<RequireDetailInitResponseBean>() {
            @Override
            public void onSuccess(RequireDetailInitResponseBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getData());
                }else{
                    Utils.showToast(context,"很抱歉,需求单不见了");
                    ((Activity)context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context,"很抱歉,需求单不见了");
                ((Activity)context).finish();
            }
        });
    }

    public void fastBindUserCustomer(final Context context, final int id, final int roleType, final RequireDetailInitResponseBean.Data data, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("requireId",String.valueOf(id));
        param.put("roleId",String.valueOf(roleType));
        param.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
        mView.showProgress();
        requireModel.fastBindUserCustomer(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response == null) return;
                if (response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response);


                }else{
                    Utils.showToast(context,response.getMessage());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context,message);
            }
        });
    }

    public void updateRequireDesign(Context context, int id, UserOBean.Data beanData, final BaseView.OnLoadListener onLoadListener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("phone",beanData.getPhone());
        param.put("name",beanData.getUserName());
        param.put("state","1");
        param.put("ucrId",String.valueOf(id));

        mView.showProgress();
        requireModel.updateRequireDesign(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    Object[] p = new Object[2];
                    p[0] = CALLBACK_TYPE_RECOVER;
                    p[1] = response.getMessage();
                    onLoadListener.onLoad(p);
                }else {
                    Object[] p = new Object[2];
                    p[0] = CALLBACK_TYPE_FAILURE;
                    p[1] = response.getMessage();
                    onLoadListener.onLoad(p);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Object[] p = new Object[2];
                p[0] = CALLBACK_TYPE_FAILURE;
                p[1] = message;
                onLoadListener.onLoad(p);
            }
        });
    }

    public void saveBidRequireInfoForRole(final Context context, int ucriId, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("ucriId",String.valueOf(ucriId));
        param.put(KEY_PHONE,SPUtils.get(context,SPConfig.PHONE,"").toString());
        if (mView == null) return;
        mView.showProgress();
        requireModel.saveBidRequireInfoForRole(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response);
                }else{
                    Utils.showToast(context,response == null ? "投标失败" : response.getMessage());
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context,"投标失败");
            }
        });
    }

    public void addRequireSubcontractInfo(Context context, int id, UserOBean.Data beanData, final BaseView.OnLoadListener onLoadListener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("designPhone",beanData.getPhone());
        param.put("designName",beanData.getUserName());
        param.put("ucriId",String.valueOf(id));

        mView.showProgress();
        requireModel.addRequireSubcontractInfo(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    Object[] p = new Object[2];
                    p[0] = CALLBACK_TYPE_RECOVER;
                    p[1] = response.getMessage();
                    onLoadListener.onLoad(p);
                }else {
                    Object[] p = new Object[2];
                    p[0] = CALLBACK_TYPE_FAILURE;
                    p[1] = response.getMessage();
                    onLoadListener.onLoad(p);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Object[] p = new Object[2];
                p[0] = CALLBACK_TYPE_FAILURE;
                p[1] = message;
                onLoadListener.onLoad(p);
            }
        });
    }

    public void getRequireOrderPrice(final Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("requireId", String.valueOf(id));
        mView.showProgress();
        orderModel.searchPriceDesignPhoneForCustomer(context, param, new CallBack<RequireOrderBean>() {
            @Override
            public void onSuccess(RequireOrderBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                } else {
                    Utils.showToast(context, "很抱歉，需求单不见了~");
                    ((Activity) context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context, "很抱歉，需求单不见了~");
                ((Activity) context).finish();
            }
        });
    }
}
